function initFollowButton(type, id, name, subTitle, skipHandler)
{
	var favoriteBtn = $('#'+type + '_' + id);
	var isFavorite = getIsFavorite(type, id, name)
	if(isFavorite)
	{
		favoriteBtn.addClass('favoriteOn');
		favoriteBtn.html('Verwijder als favoriet');
	}
	else
	{
		favoriteBtn.removeClass('favoriteOn');
		favoriteBtn.html('Voeg toe als favoriet');
	}
	
	if(!skipHandler)
	{
		favoriteBtn.click(function()
		{
			
			if(getIsFavorite(type, id, name))
			{
				removeFavorite(type, id, name);
			}
			else
			{
				addFavorite(type, id, name, subTitle);
			}
			initFollowButton(type, id, name, subTitle, true);
		});
	}
}


function getIsFavorite(type, nefubId, name)
{
	var isFavorite = false;
	if(localStorage[type])
	{
		var favorites = JSON.parse(localStorage[type]);
		if(favorites[nefubId])
		{
			isFavorite = true;
		}
	}
	return isFavorite;
}

function addFavorite(type, nefubId, name, subTitle)
{
	if(!getIsFavorite(type, nefubId, name))
	{
		var favorites = {};		 
		if(localStorage[type])
		{
			favorites = JSON.parse(localStorage[type]);
		}		
		favorites[nefubId] = {name: name, subTitle:subTitle};
		localStorage[type] = JSON.stringify(favorites);
	}
}
function removeFavorite(type, nefubId, name)
{
	if(getIsFavorite(type, nefubId, name))
	{
		var favorites = {};
		if(localStorage[type])
		{
			favorites = JSON.parse(localStorage[type]);
		}
		
		favorites[nefubId] = null;
		localStorage[type] = JSON.stringify(favorites);
	}
}

var suggestQuery = '';
var suggestLoaded = 'no';
var suggestObject;
$(document).ready(function()
{
	initZoomiOS();
	initFavoritesList();
	initSearch();
	initShowAll();

});

function initZoomiOS()
{
	if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i)) 
	{
		var viewportmeta = document.querySelector('meta[name="viewport"]');
		if (viewportmeta) 
		{
			viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
			document.body.addEventListener('gesturestart', function() 
			{
				viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
			}, false);
		}
	}
}
function initShowAll()
{
	if($('#allResults'))
	{
		$('#allResults').click(function()
		{
			$('table.results tr.hidden').removeClass('hidden');
			$('#allResults').parent().parent().hide();
			return false;
		});
		
	}
	
	if($('#allSchedule'))
	{
		$('#allSchedule').click(function()
		{
			$('table.schedule tr.hidden').removeClass('hidden');
			$('#allSchedule').parent().parent().hide();
			return false;
		});
		
	}
	

};

function initSearch()
{
	$('#search').keyup(function()
	{
		autoComplete(this.value);
	});
	autoComplete(this.value);	
}

function autoComplete(query)
{
	suggestQuery = query;
	if(suggestQuery && suggestQuery.length > 1) {
		if(suggestLoaded == 'yes') {
			doMatch();
		}
		if(suggestLoaded == 'busy') {
			// niks doen
		}
		if(suggestLoaded == 'no') {
			suggestLoaded = 'busy';
			
			$('#searchResults').html('<table><tr><td>Spelers en teams zoeken...</td></tr></table>').show();
			$.ajax({
				url: '/ajax/search/'+query.substr(0,2),
				success: function(data) {
					suggestObject = jQuery.parseJSON(data);
					suggestLoaded = 'yes';
					doMatch();
				},
				error: function(request, status, error) {
					$('#searchResults').html('Er ging iets mis bij het zoeken');
					suggestLoaded = 'no';
				}
			});
		}
	} else {
		$('#searchResults').hide();
		suggestLoaded = 'no';
		$('#searchResults').html('');
	}
}
		
function doMatch() {
	var numMatches = 0;
	var table = document.createElement('table');
	var tableRow;
	var tableCell;
	$('#searchResults').empty();
	
	$(suggestObject.results).each(function()
			{
		if(this.Name.search(new RegExp(suggestQuery, 'i')) > -1)
		{		
			tableRow = document.createElement('tr');
			tableCell = document.createElement('td');
			var anchor = document.createElement('a');
			var bold = document.createElement('b');
			
			table.appendChild(tableRow);
			tableRow.appendChild(tableCell);
			tableCell.appendChild(anchor);
			anchor.appendChild(bold);
			
			$(bold).html(this.Name);							
			anchor.setAttribute('href', this.URL);
			if(this.Subtitle)
			{
				anchor.appendChild(document.createElement('br'));
				$(anchor).append(this.Subtitle);
			}
			numMatches++;
		}
	});
	
	if(numMatches == 0)
	{
		tableRow = document.createElement('tr');
		tableCell = document.createElement('td');
		$(tableCell).addClass('noresults');
		table.appendChild(tableRow);
		tableRow.appendChild(tableCell);
		tableCell.innerHTML = "Niets gevonden voor '"+suggestQuery+"'";
	}
	
	$('#searchResults').append(table);
}


function initFavoritesList()
{
	if($('#favorites').length)
	{
		var emptied = false;
		emptied = initTypeFavoritesList('person', 'persoon', emptied);
		emptied = initTypeFavoritesList('team', 'team', emptied);
	}
}
function initTypeFavoritesList(type, href, emptied)
{
	var favorites = {};
	var hasFavorites = false;
	if(localStorage[type])
	{
		favorites = JSON.parse(localStorage[type]);
	}
	
	for(var id in favorites)
	{
		var favorite = favorites[id];
		if(favorite)
		{			
			if(!emptied)
			{
				$('#favorites').empty();
				emptied = true;
			}
			var table = document.getElementById('favorites');
			tableRow = document.createElement('tr');
			tableCell = document.createElement('td');
			table.appendChild(tableRow);
			tableRow.appendChild(tableCell);
			var anchor = document.createElement('a');
			var h4 = document.createElement('h4');
			anchor.appendChild(h4);
			tableCell.appendChild(anchor);
			
			h4.innerHTML = favorite.name;
			if(favorite.subTitle)
			{
				anchor.innerHTML +=(favorite.subTitle);
			}
			anchor.setAttribute('href', '/'+href+'/'+id);
			hasFavorites = true;
		}
	}	
	return emptied;
}


